package com.jindun.paper.ui.report;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Year;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.experimental.chart.swt.ChartComposite;

public class SourceLocationReportView extends ViewPart {

	@Override
	public void createPartControl(Composite parent) {
		
		Composite container = new Composite(parent, SWT.None);
		container.setLayout(new FillLayout());
		
		ChartComposite chartComposite = new ChartComposite(container, SWT.None, this.createChart());
		
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}
	
	private JFreeChart createChart () {
		return createChart(createDataset());
	}

	

    private static JFreeChart createChart(IntervalXYDataset intervalxydataset)
    {
        JFreeChart jfreechart = ChartFactory.createXYBarChart("State Executions - USA", "Year", true, "Number of People", intervalxydataset, PlotOrientation.VERTICAL, true, false, false);
        jfreechart.addSubtitle(new TextTitle("Source: http://www.amnestyusa.org/abolish/listbyyear.do"));
        XYPlot xyplot = (XYPlot)jfreechart.getPlot();
        XYBarRenderer xybarrenderer = (XYBarRenderer)xyplot.getRenderer();
        StandardXYToolTipGenerator standardxytooltipgenerator = new StandardXYToolTipGenerator("{1} = {2}", new SimpleDateFormat("yyyy"), new DecimalFormat("0"));
        xybarrenderer.setBaseToolTipGenerator(standardxytooltipgenerator);
        xybarrenderer.setMargin(0.10000000000000001D);
        DateAxis dateaxis = (DateAxis)xyplot.getDomainAxis();
        dateaxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
        dateaxis.setLowerMargin(0.01D);
        dateaxis.setUpperMargin(0.01D);
        ChartUtilities.applyCurrentTheme(jfreechart);
        return jfreechart;
    }

    private static IntervalXYDataset createDataset()
    {
        TimeSeries timeseries = new TimeSeries("Executions", "Year", "Count");
        try
        {
            timeseries.add(new Year(1976), new Integer(0));
            timeseries.add(new Year(1977), new Integer(1));
            timeseries.add(new Year(1978), new Integer(0));
            timeseries.add(new Year(1979), new Integer(2));
            timeseries.add(new Year(1980), new Integer(0));
            timeseries.add(new Year(1981), new Integer(1));
            timeseries.add(new Year(1982), new Integer(2));
            timeseries.add(new Year(1983), new Integer(5));
            timeseries.add(new Year(1984), new Integer(21));
            timeseries.add(new Year(1985), new Integer(18));
            timeseries.add(new Year(1986), new Integer(18));
            timeseries.add(new Year(1987), new Integer(25));
            timeseries.add(new Year(1988), new Integer(11));
            timeseries.add(new Year(1989), new Integer(16));
            timeseries.add(new Year(1990), new Integer(23));
            timeseries.add(new Year(1991), new Integer(14));
            timeseries.add(new Year(1992), new Integer(31));
            timeseries.add(new Year(1993), new Integer(38));
            timeseries.add(new Year(1994), new Integer(31));
            timeseries.add(new Year(1995), new Integer(56));
            timeseries.add(new Year(1996), new Integer(45));
            timeseries.add(new Year(1997), new Integer(74));
            timeseries.add(new Year(1998), new Integer(68));
            timeseries.add(new Year(1999), new Integer(98));
            timeseries.add(new Year(2000), new Integer(85));
            timeseries.add(new Year(2001), new Integer(66));
            timeseries.add(new Year(2002), new Integer(71));
            timeseries.add(new Year(2003), new Integer(65));
            timeseries.add(new Year(2004), new Integer(59));
            timeseries.add(new Year(2005), new Integer(60));
        }
        catch(Exception exception)
        {
            System.err.println(exception.getMessage());
        }
        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries);
        return timeseriescollection;
    }
	
}
